﻿using Book.Data;
using Book.Entities;
using Book.IRepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Book.Repositiories
{
    public class ArticleRepository : IArticleRepository
    {
        private readonly BookDbContext _DbContext;

        public ArticleRepository(BookDbContext bookDbContext)
        {
            this._DbContext = bookDbContext;
        }
        /// <summary>
        /// 添加文章
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="author"></param>
        /// <param name="menuid"></param>
        /// <param name="markdown"></param>

        public void AddArticle(string title, string content, string author, string menuid, string markdown)
        {
            ArticleEntity articleEntity = new ArticleEntity
            {
                Id = Guid.NewGuid().ToString("N").Substring(0, 29),
                Title = title,
                Content = content,
                Time = DateTime.Now.ToString(),
                Author = author,
                MenuId = menuid,
                Markdown=markdown
            };
            _DbContext.article.Add(articleEntity);
            _DbContext.SaveChanges();
        }
        /// <summary>
        /// 根据菜单ID查文章
        /// </summary>
        /// <param name="menuid"></param>
        /// <returns></returns>
        public List<ArticleEntity> GetArticleByParentId(string menuid)
        {
            ArticleEntity articleEntity = new ArticleEntity
            {
                MenuId = menuid
            };
            List<ArticleEntity> allarticleList = _DbContext.article.ToList();
            List<ArticleEntity> articleList = new List<ArticleEntity>();
            var articleresult =
                from article in allarticleList
                where article.MenuId == menuid
                orderby article.Time descending
                select article;
            foreach (ArticleEntity article in articleresult)
            {
                articleList.Add(new ArticleEntity
                {
                    Id = article.Id,
                    Title = article.Title,
                    Content = article.Content,
                    Author = article.Author,
                    MenuId = article.MenuId,
                    Time=article.Time
                });
            }
            return articleList;
        }
        /// <summary>
        /// 根据文章ID获取文章
        /// </summary>
        /// <param name="articleid"></param>
        /// <returns></returns>
        public ArticleEntity GetArticleByAId(string articleid)
        {
            ArticleEntity articleEntity = _DbContext.article.Find(articleid);
            return articleEntity;
        }
        public List<ArticleEntity> GetAllNews()
        {
            List<ArticleEntity> allarticleList = _DbContext.article
                .Where(x => x.MenuId == "000000" || x.MenuId == "111111")
                .ToList();
            foreach(ArticleEntity article in allarticleList)
            {
                article.Time = article.Time.Substring(0, 10);
            }
            return allarticleList;
        }
        /// <summary>
        /// 获得所有文章
        /// </summary>
        /// <returns></returns>
        public List<ArticleEntity> GetAllArticle()
        {
            List<ArticleEntity> allarticleList = _DbContext.article
                .Where(x=>x.MenuId!="000000"&&x.MenuId!="111111")
                .ToList();
            foreach (ArticleEntity article in allarticleList)
            {
                article.Time = article.Time.Substring(0, 10);
            }
            return allarticleList;
        }
        /// <summary>
        /// 删除文章
        /// </summary>
        /// <param name="id"></param>
        public void DeleteArticle(string id)
        {
            ArticleEntity articleEntity = _DbContext.article.Find(id);
            _DbContext.article.Remove(articleEntity);
            _DbContext.SaveChanges();
        }
        /// <summary>
        /// 修改文章
        /// </summary>
        /// <param name="id"></param>
        /// <param name="title"></param>
        /// <param name="author"></param>
        /// <param name="content"></param>
        /// <param name="markdown"></param>
        public void UpdateArticle(string id, string title, string author, string content,string markdown)
        {
            ArticleEntity articleEntity = _DbContext.article.Find(id);
            articleEntity.Title = title;
            articleEntity.Author = author;
            articleEntity.Time = DateTime.Now.ToString();
            articleEntity.Content = content;
            articleEntity.Markdown = markdown;
            _DbContext.article.Update(articleEntity);
            _DbContext.SaveChanges();
        }
    }
}
